<?php

/*
 * CopyRight  : (C)2012-2099 HaoTeam Inc.
 * Document   : RedisService.php
 * Created on : 2021-1-26 16:12:04
 * Author     : Tiger <1192851302@qq.com>
 * Description: This is NOT a freeware, use is subject to license terms.
 *              这即使是一个免费软件,使用时也请遵守许可证条款,得到当时人书面许可.
 *              未经书面许可,不得翻版,翻版必究;版权归属 HaoTeam Inc;
 */

namespace haoteam\service;

/**
 * 
 * Class ServiceRedis
 * Tiger <1192851302@qq.com> 2021-1-26 16:12:04
 */
class RedisService extends \Redis {

    /**
     * @var object 对象实例
     */
    protected static $redis;

    /**
     * 连接配置
     * @var array $options
     */
    protected static $options = [];

    /**
     * 实例化连接Redis
     * @param array $options
     * @return static
     * @access public
     */
    public static function instance($options = []) {
        if (!is_object(self::$redis)) {
            if (!extension_loaded('redis')) {
                throw new \BadFunctionCallException('not support: redis');
            }
            self::$options = include(config_path() . 'redis.php');
            if (!empty($options)) {
                self::$options = array_merge(self::$options, $options);
            }
            $func = self::$options['persistent'] ? 'pconnect' : 'connect';
            self::$redis = new static();

            self::$redis->$func(self::$options['host'], self::$options['port'], self::$options['timeout']);

            if ('' != self::$options['password']) {
                self::$redis->auth(self::$options['password']);
            }

            if (0 != self::$options['select']) {
                self::$redis->select(self::$options['select']);
            }
        }
        return self::$redis;
    }

    /**
     * 执行自增计数并获取自增后的数值
     * @param  String $key  保存计数的键值
     * @param  Int    $incr 自增数量，默认为1
     * @return Int
     */
    public function setInc($key, $incr = 1) {
        return intval($this->incr($key, $incr));
    }

    /**
     * 执行自减计数并获取自减后的数值
     * @param  String $key  保存计数的键值
     * @param  Int    $incr 自减数量，默认为1
     * @return Int
     */
    public function setDec($key, $incr = 1) {
        return intval($this->decrBy($key, $incr));
    }

    /**
     * 获取数据
     * @param type $key
     * @return type
     */
    public function query($key) {
        $data = $this->get($key);
        if (!empty($data)) {
            return json_decode($data, true);
        }
        return false;
    }

    /**
     * 写入信息
     * @author Tiger <1192851302@qq.com>
     * @param type $key
     * @param type $data
     * @param type $expire Description
     * @return boolean
     */
    public function create($key, $data, $expire = '') {
        $_expire = $expire ?: config('session')['expire'];
        $this->setex($key, $_expire, json_encode($data, JSON_UNESCAPED_UNICODE));
        return true;
    }

    /**
     * 删除
     * @author Tiger <1192851302@qq.com>
     * @param type $key
     * @return type
     */
    public function remove($key) {
        return $this->expire($key, 0); //消失吧
    }

    /**
     * 创造消息通知队列
     * @author Tiger <1192851302@qq.com>
     * @param type $type        消息类型
     * @param type $uid         客户端UID
     * @param type $message    消息(数据库整条数据)
     * @return type
     */
    public function create_client_queue($type, $uid, $message) {
        return $this->rPush("{$type}:{$uid}", json_encode(['type' => $type, 'uid' => $uid, 'message' => $message], JSON_UNESCAPED_UNICODE));
    }

    /**
     * 消费客户端队列
     * @author Tiger <1192851302@qq.com>
     * @param type $type    消息类型
     * @param type $uid     客户端UID
     * @return boolean
     */
    public function use_client_queue($type, $uid) {
        $value = $this->lPop("{$type}:{$uid}");
        if (empty($value)) {
            return false;
        }
        $item = json_decode($value, true);
        if ($item) {
            return [0, $item];
        }
    }
}
